{% extends "admin/admin_layout.html" %}
{% block admin_title %}管理角色{% endblock %}
{% block admin_head %}
{% endblock %}
{% set perm = request.perm_const %}
{% block admin_content %}
<div class="sect">
	<h2>角色列表
    	<span class="sub">共{{ page.paginator.count }}个角色</span>
        <span class="extra">
        	{% if perm.check_perm_op(perm.perm_role,perm.op_add) %}
        	<a href="javascript:;" class="act_add" onclick="add_role(event);">添加角色</a>
        	{% endif %}
        </span>
    </h2>
    <div class="sectContent">
    	<table id="role_table" class="gTable">
        	<tr>
            	<th class="m1"></th>
                <th class="m4">ID</th>
                <th class="m8">角色名</th>
                <th class="m0">用户列表</th>
                <th class="m6">编辑权限</th>
                <th class="m6">编辑用户</th>
            </tr>
            {% for obj in page.object_list %}
            <tr{% if loop.index % 2 == 0 %} class="odd"{% endif %}>
            	<td class="m1">
                	{% if obj.id > 3%}
                	<input type="checkbox" autocomplete="off" value="{{ obj.id }}"  id="chk_{{ obj.id }}" />
                    {% endif %}
                </td>
                <td class="m4">{{ obj.id }}</td>
                <td class="m8">
                	{% if perm.check_perm_op(perm.perm_role,perm.op_change) %}
                	<a href="javascript:;" onclick="edit_role(event,'{{ obj.id }}','{{ obj.name }}');">{{ obj.name }}</a>
                	{% else %}{{obj.name}}{% endif %}
                </td>
                <td>{% set uss = obj.getUsers() %}
                    {% for u in uss %}
                    <a href="/user/{{ u.id }}/">{{ u.nick_name }}</a>
                    (<span class="hint">{{ "%s  %s"|format(u.name,request.utils.getGenderArea(u,False)) }}</span>)<br />
                    {% endfor %}
                </td>
                <td class="m6">
                	{% if perm.check_perm_op(perm.perm_role,perm.op_change) %}
                	<a href="/admin/role/{{ obj.id }}/perms/">编辑权限</a>
                	{% endif %}
                </td>
                <td class="m6">
                	{% if perm.check_perm_op(perm.perm_role,perm.op_change) %}
                	<a href="/admin/role/{{ obj.id }}/users/">编辑用户</a>
                	{% endif %}
                </td>
            </tr>
            {% endfor %}
            <tr>
            	<td><input type="checkbox" autocomplete="off" id="chkAll" onclick="$checkAll('role_table','chkAll');" /></td>
                <td colspan="5">
                	{% if perm.check_perm_op(perm.perm_role,perm.op_del) %}
                    <input type="button" class="button btns" value="删除选中" onclick="del_role('role_table','chkAll');" />
                    {% endif %}
                </td>
            </tr>
        </table>
        {% include "paginator.html" %}
    </div>
</div>
{% endblock %}
{% block popup_content %}
    <div id="s_role_div" class="popup" style="display:none;">
    	<div class="sect">
        	<h2>
            	<span id="s_role_title" class="title"></span>
                <span class="extra" onclick="$('s_role_div').tHide();">关闭</span>
            </h2>
            <div class="sectContent gForm">
            	<div id="id_row" class="row">
                	<label>角色ID：</label>
                    <input type="text" class="inputbox" disabled="disabled" id="role_id" />
                </div>
            	<div id="name_row" class="row">
                	<label>角色名：</label>
                    <input type="text" class="inputbox" id="role_name" />
                </div>
                <div class="sectBtn">
                	<input type="button" class="button" value="确定" onclick="do_role('role_id','role_name');" />
                    <a href="javascript:;" onclick="$('s_role_div').tHide();">取消</a>
                </div>
            </div>
        </div>
    </div>
<script type="text/javascript">
function add_role(ev){
    $("s_role_title").innerHTML = "添加角色";$("id_row").style.display = "none";$("role_id").value = "";$("role_name").value = "";
    $('s_role_div').fshow({event:ev});
}
function edit_role(ev, id, name){
    $("s_role_title").innerHTML = "修改角色";$("id_row").style.display = "block";$("role_id").value = id;$("role_name").value = name;
    $('s_role_div').fshow({event:ev});
}
function do_role(id_input, name_input){
    var to_url = "";var id = $(id_input).value;var name = $(name_input).value;var params = "";
    if (!name) {new MsgBox({message:"你还未输入角色名"}); return;}
    if (!id) {to_url = "/admin/role/add/";params = "name=" + name;}
    else {to_url = "/admin/role/edit/";params = String.format("id={0}&name={1}",id,String.encode(name));}
    new echoAjax({url:to_url, params:params,reLoad:true}).fire();
}
function del_role(parent, chkall){
    if (!confirm("你确定要删除选中的内容吗？"))return;
    var ids = $getCheckValues(parent, chkall);
    if (!ids) {new MsgBox({message:"没有选中任何内容"}); return;}
    var params = "value=" + ids;
    new echoAjax({url:"/admin/role/delete/", params:params,reLoad:true}).fire();
}
</script>
{% endblock %}
